package com.xindaoapp.happypet.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.PhotoItem;
import com.xindaoapp.happypet.utils.Constants;

/**
 * 查看大图片
 * 
 * @author
 * 
 */
public class PicturePreviewActivity extends UMBaseActivity {
    private TextView title;
    private TextView selected;
    private CheckBox checkBox;
    private ArrayList<PhotoItem> list;
    private ArrayList<PhotoItem> selectedList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_picture_preview);
        title = (TextView) findViewById(R.id.title);
        selected = (TextView) findViewById(R.id.title_right);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        list = (ArrayList<PhotoItem>) getIntent().getSerializableExtra(Constants.KEY_PHOTOS);
        selectedList =
                (ArrayList<PhotoItem>) getIntent().getSerializableExtra(
                        Constants.KEY_PHOTOS_SELECTED);
        viewPager.setAdapter(new MyGalleryAdapter(list));
        viewPager.setCurrentItem(getIntent().getIntExtra(Constants.KEY_CURRENT_INDEX, 0));
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                resetCurrentIndex(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
    }

    private void resetCurrentIndex(int position) {
        title.setText(String.format("%s/%s", position, list.size()));
        selected.setText(String.format("完成%s/%s", selectedList.size(),
                Constants.PUBLISHPOSTS_COUNTS));
        if (list.get(position).select) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }
    }

    class MyGalleryAdapter extends PagerAdapter {
        private ArrayList<PhotoItem> list = new ArrayList<PhotoItem>();

        public MyGalleryAdapter(ArrayList<PhotoItem> list) {
            super();
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view =
                    LayoutInflater.from(getBaseContext()).inflate(R.layout.picture_edit_item, null);
            ImageLoader.getInstance().displayImage("file://" + list.get(position).imageFilePath,
                    (ImageView) view.findViewById(R.id.imageview));
            container.addView(view);
            return view;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }

}
